/**
 * Collaborative Story Development Web Application (CSDApp) Copyright Anton
 * Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.sql.Connection;
import java.sql.Timestamp;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The controller class handles user initiated requests for the particular
 * resource and prepares the correct view.
 *
 * @author Anton Strack
 */
public class RelationshipController extends ResourceController {

    private final String VIEW_LIST = "/relationship/index.jsp";
    private final String VIEW_VIEW = "/relationship/view.jsp";
    private final String VIEW_CREATE = "/relationship/create.jsp";
    private final String VIEW_UPDATE = "/relationship/update.jsp";
    private final String VIEW_DELTE = "/relationship/delete.jsp";
    private final String VIEW_ADD = "/relationship/add.jsp";
    private final String VIEW_LIST_POSTS = "/relationship/listPosts.jsp";

    public RelationshipController(Connection conn) {
        super(conn);
    }

    /**
     * This method primarily handles form related requests.
     *
     * @param request the request object
     * @return an updated request object sufficient to complete the request.
     */
    @Override
    public CsdwaRequest handlePostRequest(CsdwaRequest request) {
        String form = request.getFormId();
        String primaryAction = request.getPrimaryActionTypeName();
        String primaryResource = request.getPrimaryResourceTypeName();
        String postId = request.getParameter("secondaryId");
        request.setPrimaryResourceId(postId);
        int id = request.getPrimaryResourceId();
        if (form.isEmpty() == false) {
            request.setPrimaryResourceId(request.getParameter("postId"));
            if (form.equalsIgnoreCase("create-relationship")) {
                request.removeFormId();
                request = handleCreate(request);
            } else if (form.equalsIgnoreCase("update-relationship")) {
                request.removeFormId();
                request = handleUpdate(request);
            } else if (form.equalsIgnoreCase("delete-relationship")) {
                request.removeFormId();
                request = handleDelete(request);
            } else if (form.equalsIgnoreCase("add-relationship")) {
                request.removeFormId();
                request = handleAdd(request);
            }
        }
        return request;
    }

    /**
     * This method handles get requests. It accepts the primaryResouce and
     * primaryAction as variables to allow routing from the handleGetRequest
     * method.
     *
     * @param request the request object
     * @return an updated request object sufficient to complete the request.
     */
    @Override
    public CsdwaRequest handleGetRequest(CsdwaRequest request) {
        String primaryAction = request.getPrimaryActionTypeName();
        String primaryResource = request.getPrimaryResourceTypeName();
        String secondaryResource = request.getSecondaryResourceTypeName();
        String secondaryAction = request.getSecondaryActionTypeName();
        String tertiaryId = request.getParameter("tid");
        int secondaryId = request.getSecondaryResourceId();
        int id = request.getPrimaryResourceId();
        if ("relationship".equalsIgnoreCase(primaryResource)) {
            if ("view".equalsIgnoreCase(primaryAction) && id != 0) {

                RelationshipDao rdao = new RelationshipDao(conn);
                rdao.setUseRelatedMarkers(true);
                int result = rdao.selectBeanById(id);
                if (result > 0) {
                    request.setBean(rdao.getBean());
                    request.setView(VIEW_VIEW);
                } else {
                    request.setMessage("The relationship couldn't be found.");
                    request.setMessageType("error");
                    request.setPrimaryActionTypeName("list");
                    request.setPrimaryResourceTypeName("relationship");
                    request.setPrimaryResourceId(0);
                    request = handleGetRequest(request);
                }
                //  this.getResourceRequestDelegator().getUserHistoryManager().recordUserPostHistory(request.getCurrentUser(), InfoAggregator.getInstance().getActionTypeId("view"), rdao.getBean());
            } else if ("update".equalsIgnoreCase(primaryAction) && id != 0) {

                RelationshipDao rdao = new RelationshipDao(conn);
                rdao.selectBeanById(id);
                /* Store the bean in a session so that the it is available to the jsp page and also post update handler on post submit. This way it can compare the post bean values to the original bean values. Doing so will mean that only the columns changed will be sent to the database to be updated.
                 */
                int result = rdao.selectBeans();
                if (result > 0) {
                    request.setView(VIEW_UPDATE);
                    request.removeSessionAttribute("bean");
                    request.setSessionAttribute("bean", rdao.getBean());
                } else {
                    request.setMessage("The relationship couldn't be found.");
                    request.setMessageType("error");
                    request.setPrimaryActionTypeName("list");
                    request.setPrimaryResourceTypeName("relationship");
                    request.setPrimaryResourceId(0);
                    request = handleGetRequest(request);
                }
            } else if ("create".equalsIgnoreCase(primaryAction)) {
                request.setView(VIEW_CREATE);
                ResourceTypeDao rtDao = new ResourceTypeDao(conn);
                rtDao.selectResourceTypesByParentId(InfoAggregator.getInstance().getResourceTypeId("relationship"));
                request.setAttribute("resourceTypes", rtDao.getList());
            } else if ("delete".equalsIgnoreCase(primaryAction)) {

                RelationshipDao rdao = new RelationshipDao(conn);
                int result = rdao.selectBeanById(id);
                // will need to delete all category mappings to plots

                if (result > 0) {
                    request.setBean(rdao.getBean());
                    request.setView(VIEW_DELTE);
                } else {
                    request.setMessage("The relationship couldn't be found.");
                    request.setMessageType("error");
                    request.setPrimaryActionTypeName("list");
                    request.setPrimaryResourceTypeName("relationship");
                    request.setPrimaryResourceId(0);
                    request = handleGetRequest(request);
                    Logger.getLogger(TaskController.class.getName()).log(Level.SEVERE, "No relationship found in the database.");
                }
            } else if ("add".equalsIgnoreCase(primaryAction) && id > 0 && secondaryResource != null && secondaryResource.isEmpty() == false) {
                // action=add&resource=relationship&for=plot&and=character
                RelationshipDao rdao = new RelationshipDao(conn);
                // need to convert
                int result = rdao.selectRelationshipsByResourceTypeId(id);
                System.out.println("relationship count is:" + result);

                RelationshipManager relationshipManager = new RelationshipManager(conn);
                RelatedMapperDao rmDao = relationshipManager.getRelatedMapperDao(id);
                InfoAggregator infoAggregator = InfoAggregator.getInstance();

                int primaryTypeId = infoAggregator.getResourceTypeId(secondaryResource);
                if (rmDao != null) {
                    CsdwaDao postADao = rmDao.getPostADao();
                    CsdwaDao postBDao = rmDao.getPostBDao();
                    // Check that the daos are properly selected. Also, make sure that the primaryTypeId is one of the dao types.
                    if (result > 0 && postADao != null && postBDao != null && (primaryTypeId == rmDao.getRelatedAResourceTypeId() || primaryTypeId == rmDao.getRelatedBResourceTypeId())) {
                        postADao.setQueryMarkerValue(QueryMarker.ORDER_BY_COLUMN, "name");
                        postADao.setQueryMarkerValue(QueryMarker.ORDER_BY_MODE, "ASC");
                        postBDao.setQueryMarkerValue(QueryMarker.ORDER_BY_COLUMN, "name");
                        postBDao.setQueryMarkerValue(QueryMarker.ORDER_BY_MODE, "ASC");
                        int aResult = postADao.selectBeans();
                        int bResult = postBDao.selectBeans();

                        if (aResult > 0 && bResult > 0) {
                            request.setBeans(rdao.getBeans());
                            request.setAttribute("aPosts", postADao.getBeans());
                            request.setAttribute("postATypeName", infoAggregator.getResourceTypeName(rmDao.getRelatedAResourceTypeId()));
                            request.setAttribute("postATypeId", rmDao.getRelatedAResourceTypeId());
                            request.setAttribute("resourceTypeId", id);
                            request.setAttribute("bPosts", postBDao.getBeans());
                            request.setAttribute("postBTypeName", infoAggregator.getResourceTypeName(rmDao.getRelatedBResourceTypeId()));
                            request.setAttribute("postBTypeId", rmDao.getRelatedBResourceTypeId());
                            request.setAttribute("primaryTypeId", primaryTypeId);
                            request.setAttribute("primaryId", secondaryId);// secondary id points to the id of the secondary resource type.
                            request.setAttribute("postName", request.getParameter("name"));// just pass the name along to the other view
                            request.setView(VIEW_ADD);
                        } else {
                            request.setMessage("The post couldn't be found.");
                            request.setMessageType("error");
                            request.setPrimaryActionTypeName("list");
                            request.setPrimaryResourceTypeName("relationship");
                        }
                    } else {
                        request.setMessage("No relationship type was found.");
                        request.setMessageType("error");
                        request.setPrimaryActionTypeName("list");
                        request.setPrimaryResourceTypeName("relationship");
                        request.setPrimaryResourceId(0);
                        Logger.getLogger(TaskController.class.getName()).log(Level.SEVERE, "No relationship was found in the database.");
                        request = handleGetRequest(request);
                    }
                } else {
                    request.setMessage("No relationship type was found.");
                    request.setMessageType("error");
                    request.setPrimaryActionTypeName("list");
                    request.setPrimaryResourceTypeName("relationship");
                    request.setPrimaryResourceId(0);
                    Logger.getLogger(TaskController.class.getName()).log(Level.SEVERE, "No relationship found in the database.");
                    request = handleGetRequest(request);
                }
            } else if ("remove".equalsIgnoreCase(primaryAction) && secondaryResource != null && secondaryResource.isEmpty() == false && id != 0 && secondaryId > 0 && tertiaryId != null && tertiaryId.isEmpty() == false) {
                InfoAggregator infoAggregator = InfoAggregator.getInstance();

                int tid = Integer.parseInt(tertiaryId);
                RelationshipManager relationshipManager = new RelationshipManager(this.conn);
                RelatedMapperDao rmDao = relationshipManager.getRelatedMapperDao(id);
                int primaryTypeId = infoAggregator.getResourceTypeId(secondaryResource);
                RelatedMap rm;
                int result = 0;
                if (rmDao != null) {
                    result = rmDao.selectBeanById(secondaryId);
                    request.setPrimaryResourceTypeName(secondaryResource);
                    request.setPrimaryResourceId(tertiaryId);
                    if (result > 0) {
                        rm = rmDao.getBean();
                        rmDao.deleteBean(rm);
                        if (result > 0) {
                            request.setMessage("The relationship was removed successfully.");
                            request.setMessageType("success");
                            request.setPrimaryActionTypeName("view");
                            request = this.ResourceRequestDelegator.processRequest(request);
                        } else {
                            request.setMessage("The relationship couldn't be deleted.");
                            request.setMessageType("error");
                            request.setPrimaryActionTypeName("view");
                            Logger.getLogger(CategoryController.class.getName()).log(Level.SEVERE, "Couldn't delete relationship mapping to post.");
                            request = this.ResourceRequestDelegator.processRequest(request);
                        }
                    }
                }

                if (result == 0) {
                    request.setMessage("The relationship couldn't be found.");
                    request.setMessageType("error");
                    request.setPrimaryActionTypeName("view");
                    request.setPrimaryResourceTypeName(secondaryResource);
                    request.setPrimaryResourceId(tertiaryId);
                    Logger.getLogger(RelationshipController.class.getName()).log(Level.SEVERE, "No relationship found in the database.");
                    request = this.ResourceRequestDelegator.processRequest(request);
                }

            } else if ("list".equalsIgnoreCase(primaryAction) && secondaryResource != null && secondaryResource.isEmpty() == false && id != 0 && secondaryId != 0) {
                //action=list&resource=relationship&id=##&for=plot&sid=###
                //                                relationshipId   postId

                ResourceType resourceType = InfoAggregator.getInstance().getResourceType(id);
                //int result = rdao.selectRelationshipsByResourceTypeId(id);
                if (resourceType != null) {
                    request.setBean(resourceType);
                    //  request.setBean(rdao.getBean());
                    RelatedMapperDao relatedPostsDao = this.ResourceRequestDelegator.getRelationshipManager().getRelatedMapperDao(id);
                    relatedPostsDao.selectRelatedBPostsByAPostId(secondaryId, true);
                    request.setAttribute("postRelationships", relatedPostsDao.getPostBDao().getBeans());
                    // need to add additional post types like above
                    request.setView(VIEW_LIST_POSTS);

                } else {
                    request.setMessage("The relationship couldn't be found.");
                    request.setMessageType("error");
                    request.setPrimaryActionTypeName("view");
                    request.setPrimaryResourceTypeName(secondaryResource);
                    request.setPrimaryResourceId(secondaryId);
                    Logger.getLogger(RelationshipController.class.getName()).log(Level.SEVERE, "No relationship found in the database.");
                    request = this.ResourceRequestDelegator.processRequest(request);
                }
            } else {// index by default
                request.setView(VIEW_LIST);
                RelationshipDao rdao = new RelationshipDao(conn);

                rdao.setUseRelatedMarkers(true);

                int result = rdao.selectBeans();
                if (result > 0) {
                    request.setBeans(rdao.getBeans());
                } else {
                    request.setMessage("There are no relationship to list.");
                    request.setMessageType("info");
                }
            }
        }
        return request;
    }

    /**
     * This method saves the bean to the database.
     *
     * @param request the create request object containing the form values.
     * @return the request object with the requestResultMessage object set to
     * indicate the result
     */
    private CsdwaRequest handleCreate(CsdwaRequest request) {
        User user = (User) request.getSessionAttribute("user");
        Timestamp timestamp = new Timestamp(new Date().getTime());
        // Create validators to use for validation
        CsdwaValidator isNotEmpty = new IsNotEmpty();
        CsdwaValidator isNumeric = new IsNumeric();
        // add request parameter values to the object to test validation
        ValidatorValue name = new ValidatorValue(request.getParameter("name"), "Name");
        ValidatorValue description = new ValidatorValue(request.getParameter("description"), "Description");
        ValidatorValue resourceType = new ValidatorValue(Integer.parseInt(request.getParameter("resourceTypeId")), "Resource Type Id");
        // the the validators to the validation values
        name.addValidator(isNotEmpty);
        description.addValidator(isNotEmpty);
        resourceType.addValidator(isNumeric).addValidator(isNotEmpty);
        // isValid the values.
        if (name.isValid() && description.isValid() && resourceType.isValid()) {

            Relationship bean = ResourceFactory.makeRelationship();
            bean.setName(name.getString());
            bean.setDescription(description.getString());
            bean.setResourceTypeId(resourceType.getInteger());

            RelationshipDao rdao = new RelationshipDao(conn);
            int result = rdao.insertBean(bean);
            if (result > 0) {
                this.getResourceRequestDelegator().getUserHistoryManager().recordUserPostHistory(request.getCurrentUser(), InfoAggregator.getInstance().getActionTypeId("create"), bean);
                bean.setId(result);
                request.setPrimaryActionTypeName("view");
                request.setPrimaryResourceTypeName("relationship");
                request.setPrimaryResourceId(result);
                request = handleGetRequest(request);
            } else {
                request.setMessage("There was an error saving the relationship to the database.");
                request.setMessageType("error");
                request.setPrimaryActionTypeName("create");
                request.setPrimaryResourceTypeName("relationship");
                request.setPrimaryResourceId(0);
                request = handleGetRequest(request);
            }
        } else {
            request.setMessage(name.getErrorMessage() + description.getErrorMessage() + resourceType.isValid());
            request.setMessageType("info");
            request.setPrimaryActionTypeName("create");
            request.setPrimaryResourceTypeName("relationship");
            request.setPrimaryResourceId(0);
            request = handleGetRequest(request);
        }
        return request;
    }

    /**
     * This method handles updating the bean. If the bean to be updated was
     * properly set as a bean in the session, then this method will instruct the
     * dao to update only the fields that were changed and thus reduce message
     * size.
     *
     * @param request the update request object
     * @return a request object with the requestResultMessage object set to
     * indicate the update result.
     */
    private CsdwaRequest handleUpdate(CsdwaRequest request) {
        User user = (User) request.getSessionAttribute("user");
        Timestamp timestamp = new Timestamp(new Date().getTime());
        Relationship originalPost = (Relationship) request.getSessionAttribute("bean");

        CsdwaValidator isNotEmpty = new IsNotEmpty();
        // add request parameter values to the object to test validation
        ValidatorValue name = new ValidatorValue(request.getParameter("name"), "Name");
        ValidatorValue description = new ValidatorValue(request.getParameter("description"), "Description");
        // the the validators to the validation values
        name.addValidator(isNotEmpty);
        description.addValidator(isNotEmpty);
        // isValid the values.
        if (name.isValid() && description.isValid()) {
            RelationshipDao cdao = new RelationshipDao(conn);
            StringBuilder sb = new StringBuilder();
            if (originalPost == null) {
                originalPost = ResourceFactory.makeRelationship();// if the original post didn't get stored in the session, then create a new one.
            }

            if (originalPost.getName().equals(name.getString()) == false) {
                sb.append("Name");
            }
            if (originalPost.getDescription().equals(description.getString()) == false) {
                if (sb.length() > 0) {
                    sb.append(", ");
                }
                sb.append("Description");
            }

            // update the original bean with all the values from the form submit
            originalPost.setName(name.getString());
            originalPost.setDescription(description.getString());
            // declare the columns to update.
            cdao.setColumnsToUpdate(sb.toString());
            int result = cdao.updateBean(originalPost);
            request.removeSessionAttribute("bean");
            if (result >= 0) {
                this.getResourceRequestDelegator().getUserHistoryManager().recordUserPostHistory(request.getCurrentUser(), InfoAggregator.getInstance().getActionTypeId("update"), originalPost);
                // success so go to view page of updated plot
                request.setMessage("The relationship was successfully updated.");
                request.setMessageType("success");
                request.setPrimaryActionTypeName("view");
                request.setPrimaryResourceTypeName("relationship");
                request.setPrimaryResourceId(originalPost.getId());
                request = handleGetRequest(request);
            } else {
                request.setMessage("There was an error trying to update the relationship to the database.");
                request.setMessageType("error");
                request.setPrimaryActionTypeName("update");
                request.setPrimaryResourceTypeName("relationship");
                request.setPrimaryResourceId(originalPost.getId());
                request = handleGetRequest(request);
            }

        } else {
            request.setMessage(name.getErrorMessage() + description.getErrorMessage());
            request.setMessageType("info");
            request.setPrimaryActionTypeName("update");
            request.setPrimaryResourceTypeName("relationship");
            request.setPrimaryResourceId(originalPost.getId());
            request = handleGetRequest(request);
        }

        return request;
    }

    /**
     * This method handles deleting the specific bean from the database. This
     * method handles the cascading deletes of all dependant resources.
     *
     * @param request the delete request object.
     * @return a request object with a requestResultMessage object specifying
     * the result.
     */
    private CsdwaRequest handleDelete(CsdwaRequest request) {
        // validate fields
        int id = request.getPrimaryResourceId();
        boolean allowDelete = false;
        if (id > 0) {
            try {

                //  conn.setAutoCommit(false);// enter transaction mode //- not sure if need transaction mode for categories yet.
                // Savepoint delete = conn.setSavepoint("delete-category_" + id);
                RelationshipDao rdao = new RelationshipDao(conn);
                if (rdao.selectBeanById(id) > 0) {
                    allowDelete = true;
                }
                if (allowDelete == true) {
                    Relationship category = rdao.getBean();
                    int result = rdao.deleteBean(category);
                    request.removeAttribute("formSubmit");
                    if (result > 0) {
                        this.getResourceRequestDelegator().getUserHistoryManager().recordUserPostHistory(request.getCurrentUser(), InfoAggregator.getInstance().getActionTypeId("delete"), rdao.getBean());
                        //   conn.setAutoCommit(true);// reset commit mode to auto and commit the transaction since the deletion worked correctly.
                        request.setMessage("The relationship was successfully deleted from the site.");
                        request.setMessageType("success");
                        request.setPrimaryActionTypeName("list");
                        request.setPrimaryResourceTypeName("relationship");
                        request.setPrimaryResourceId(0);
                        request = handleGetRequest(request);
                    } else {
                        //   conn.rollback(delete); // delete failed so roll back connection
                        //    conn.setAutoCommit(true);// reset commit mode to auto
                        request.setMessage("There was an error trying to delete the relationship from the database.");
                        request.setMessageType("error");
                        request.setPrimaryActionTypeName("view");
                        request.setPrimaryResourceTypeName("relationship");
                        request.setPrimaryResourceId(id);
                        request = handleGetRequest(request);
                    }

                } else {
                    //   conn.rollback(delete); // delete failed so roll back connection
                    //   conn.setAutoCommit(true);// reset commit mode to auto
                    request.setMessage("A dependant resource could not be deleted. Deletion aborted.");
                    request.setMessageType("error");
                    request.setPrimaryActionTypeName("view");
                    request.setPrimaryResourceTypeName("relationship");
                    request.setPrimaryResourceId(0);
                    request = handleGetRequest(request);
                }

            } catch (Exception ex) { //SQLException ex) {
                Logger.getLogger(TaskController.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            request.setMessage("There was an error with the delete request. Could not determine the relationship ID.");
            request.setMessageType("error");
            request.setPrimaryActionTypeName("list");
            request.setPrimaryResourceTypeName("relationship");
            request.setPrimaryResourceId(0);
            request = handleGetRequest(request);
        }
        return request;
    }

    /**
     * This method saves the bean to the database.
     *
     * @param request the create request object containing the form values.
     * @return the request object with the requestResultMessage object set to
     * indicate the result
     */
    private CsdwaRequest handleAdd(CsdwaRequest request) {
        // Create validators to use for validation
        CsdwaValidator isNotEmpty = new IsNotEmpty();
        CsdwaValidator isNumeric = new IsNumeric();
        // add request parameter values to the object to test validation
        ValidatorValue postAId = new ValidatorValue(request.getParameter("postA"), "post A ID");
        ValidatorValue postBId = new ValidatorValue(request.getParameter("postB"), "Post B ID");
        ValidatorValue relationshipResourceTypeId = new ValidatorValue(request.getParameter("resourceTypeId"), "Base Relationship Type");
        ValidatorValue relationshipTypeId = new ValidatorValue(request.getParameter("relationshipId"), "Relationship Type");
        ValidatorValue primaryTypeId = new ValidatorValue(request.getParameter("primaryTypeId"), "Primary Type Id");

        postAId.addValidator(isNotEmpty).addValidator(isNumeric);
        postBId.addValidator(isNotEmpty).addValidator(isNumeric);
        relationshipTypeId.addValidator(isNumeric).addValidator(isNotEmpty);
        relationshipResourceTypeId.addValidator(isNumeric).addValidator(isNotEmpty);
        primaryTypeId.addValidator(isNumeric).addValidator(isNotEmpty);

        if (postAId.isValid() && postBId.isValid() && relationshipTypeId.isValid() && relationshipResourceTypeId.isValid() && primaryTypeId.isValid()) {
            RelatedMap relatedMap;
            int result;
            RelatedMapperDao rmDao = this.getResourceRequestDelegator().getRelationshipManager().getRelatedMapperDao(relationshipResourceTypeId.getInteger());

            relatedMap = new RelatedMap();
            relatedMap.setPostBId(postBId.getInteger());
            relatedMap.setPostAId(postAId.getInteger());
            relatedMap.setRelationshipTypeId(relationshipTypeId.getInteger());
            result = rmDao.insertBean(relatedMap);

            int primaryId = postAId.getInteger();
            if (primaryTypeId.getInteger() == rmDao.getRelatedBResourceTypeId()) {
                primaryId = postBId.getInteger();
            }
            if (result > 0) {
                request.setMessage("The relationship was successfully added to the " + InfoAggregator.getInstance().getResourceTypeName(primaryTypeId.getInteger()) + ".");
                request.setMessageType("success");
                request.setPrimaryActionTypeName("view");
                request.setPrimaryResourceTypeName(InfoAggregator.getInstance().getResourceTypeName(primaryTypeId.getInteger()));
                request.setPrimaryResourceId(primaryId);
                request.setMethod("GET");
                request = this.ResourceRequestDelegator.processRequest(request);
            } else {
                request.setMessage("There was an error saving the relationship to the database.");
                request.setMessageType("error");
                request.setPrimaryActionTypeName("view");
                request.setPrimaryResourceTypeName(InfoAggregator.getInstance().getResourceTypeName(primaryTypeId.getInteger()));
                request.setPrimaryResourceId(primaryId);
                request.setMethod("GET");
                request = this.ResourceRequestDelegator.processRequest(request);
            }
        } else {
            request.setMessage(postAId.getErrorMessage() + postBId.getErrorMessage() + relationshipResourceTypeId.getErrorMessage() + relationshipTypeId.getErrorMessage() + primaryTypeId.getErrorMessage());
            request.setMessageType("info");
            request.setPrimaryActionTypeName("add");
            request.setPrimaryResourceTypeName("relationship");
            request.setSecondaryResourceTypeId(primaryTypeId.getInteger());
            request.setPrimaryResourceId(relationshipResourceTypeId.getInteger());
            request = handleGetRequest(request);
        }
        return request;
    }
}
